$(document).ready(function() {
    editPattern.init();
});

var editPattern = (function() {

    function savePattern() {
        var patternId = $('input[name=patternId]').val(),
            pName = $('input[name=patternName]').val(),
            link = $('input[name=link]').val(),
            title = $('input[name=title]').val(),
            category = $('input[name=category]').val(),
            desc = $('input[name=desc]').val(),
            content = $('input[name=content]').val(),
            journalist = $('input[name=journalist]').val(),
            thumb = $('input[name=thumb]').val(),
            published = $('input[name=published]').val(),
            tags = $('input[name=tags]').val();
        
        $('.form-group').removeClass('has-error');
        $('.errorHandler').html('');
        if (!pName) {
            $('#postTitle').parents('.form-group').addClass('has-error');
            $('#addNewPost').find('i.fa').attr('class', 'fa fa-plus-circle');
            return false;
        }
        
        $.post('/pattern/ajax-save', {
            patternId: patternId,
            pName: pName,
            link: link,
            title: title,
            category: category,
            desc: desc,
            content: content,
            journalist: journalist,
            thumb: thumb,
            published: published,
            tags: tags
        }, function(resp) {
            if (resp.err === 0) {
                window.location.href = '/pattern/edit?id=' + resp.data.id + '#msg=' + resp.msg;
                $('#saveNewPost').find('i').attr('class', 'fa fa-plus-circle');
            } else {
                $('.errorHandler').removeClass('text-success').addClass('text-danger').html(resp.msg).show();
                $('#saveNewPost').find('i').attr('class', 'fa fa-plus-circle');
            }
        }, 'json');
    }
    function deletePattern(id, url){
        showConfirm({
            msg: 'Bạn có chắc chắn muốn xoá mẫu tin này không? <strong class="text-danger">' + url +'</strong>',
            title: 'Xoá Tin tức',
            noBt: 'Đóng',
            yesBt: 'Đồng ý xoá',
            onYes: function() { 
                $.post('/pattern/ajax-delete', {id: id}, function(resp) {
                    if(resp.err === 0){
                        hideConfirm();
                    }else {
                         hideConfirm();
                        $('.errorHandler').removeClass('text-success').addClass('text-danger').html(resp.msg).show();
                    }
                }, 'json');
            }
        });
    }

    function initHanlder() {
        $('#savePattern').click(function() {
            $(this).find('i.fa').attr('class', 'fa fa-spinner fa-spin');
            savePattern();
            return false;
        });
        $('a.delete-pattern').click(function(e) {
            e.preventDefault();
            var patternId = $(this).data('patternid');
            var patternName = $(this).data('patternname');
            deletePattern(patternId, patternName);
            return false;
        });
    }
    return {
        init: function() {
            initHanlder();
        }
    };
})();